﻿using Microsoft.Phone.Shell;
using System.ComponentModel;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Windows.Threading;
using System.Windows.Media;
using System;

namespace AccountHelper
{
    /// <summary>
    /// Source Code from jeff
    /// http://www.jeff.wilcox.name/2011/07/creating-a-global-progressindicator-experience-using-the-windows-phone-7-1-sdk-beta-2/
    /// rewrite by Alexis 20110812
    /// </summary>
    public class GlobalLoading : INotifyPropertyChanged
    {
        //Progress and Text Indicator
        private ProgressIndicator _mangoIndicator;

        //Timer for play message
        private DispatcherTimer _timer;

        //Current PhoneApplication
        private PhoneApplicationPage CurrentPage;

        //Ad Words
        private string[] AdvWords;

        private GlobalLoading()
        {
            AdvWords = new string[] { 
                "SystemTray是个好东东哦,亲",
                "她可以用来打广告的哦",
                "她也可以用来显示进度",
                "至于你信不信",
                "I do believe!!!"
            };
        }

        public void Initialize(PhoneApplicationFrame frame)
        {
            _mangoIndicator = new ProgressIndicator();

            frame.Navigated += OnRootFrameNavigated;
        }

        private void OnRootFrameNavigated(object sender, NavigationEventArgs e)
        {
            // Use in Mango to share a single progress indicator instance.
            var ee = e.Content;
            var pp = ee as PhoneApplicationPage;
            CurrentPage = pp;
            if (pp != null)
            {
                pp.SetValue(SystemTray.ProgressIndicatorProperty, _mangoIndicator);
                pp.SetValue(SystemTray.OpacityProperty, 0.9);
                pp.SetValue(SystemTray.BackgroundColorProperty, Color.FromArgb(255, 0, 0, 0));
                pp.SetValue(SystemTray.ForegroundColorProperty, Color.FromArgb(0, 255, 255, 255));
            }
        }

        private static GlobalLoading _in;
        public static GlobalLoading Instance
        {
            get
            {
                if (_in == null)
                {
                    _in = new GlobalLoading();
                }

                return _in;
            }
        }

        private bool _isBusy;
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
                if (_mangoIndicator == null)
                {
                    _mangoIndicator = new ProgressIndicator();
                }
                if (CurrentPage != null)
                {
                    CurrentPage.SetValue(SystemTray.IsVisibleProperty, value);
                    _mangoIndicator.IsVisible = value;
                    _mangoIndicator.IsIndeterminate = value;
                    _mangoIndicator.Text = "";
                }

            }
        }

        private bool _isStartAdv;
        public bool IsStartAdv
        {
            get
            {
                return _isStartAdv;
            }
            set
            {
                _isStartAdv = value;
                _mangoIndicator.IsVisible = value;
                if (CurrentPage != null)
                {
                    CurrentPage.SetValue(SystemTray.IsVisibleProperty, value);
                }
                if (value)
                {
                    StartTimer();
                }
                else
                {
                    StopTimer();
                }
            }
        }


        private int Index = 0;//for auto play use

        private void StartTimer()
        {
            if (null == _timer)
            {
                _timer = new DispatcherTimer();
            }
            _timer.Interval = TimeSpan.FromSeconds(2);
            _timer.Tick += (sender, e) =>
            {
                Index++;
                if (Index == AdvWords.Length)
                {
                    Index = 0;
                }
                _mangoIndicator.Text = AdvWords[Index];
            };
            _timer.Start();
        }

        private void StopTimer()
        {
            if (_timer != null)
            {
                _timer.Stop();
                _timer = null;
            }
        }

        #region Error Use

        public bool IsPlayAdv
        {
            set
            {
                _mangoIndicator.IsVisible = value;
                if (CurrentPage != null)
                    CurrentPage.SetValue(SystemTray.IsVisibleProperty, value);

                if (value)
                {
                    if (null == _timer)
                        _timer = new DispatcherTimer();

                    _timer.Tick += (sender, e) =>
                        {
                            BeginTimer();
                        };
                    _timer.Start();
                }
                else
                    StopTimer();
            }
        }

        private void BeginTimer()
        {
            for (int i = 0; i < AdvWords.Length; i++)
            {
                if (i == AdvWords.Length - 1)
                {
                    i = 0;
                }
                _mangoIndicator.Text = AdvWords[i];
            }
        }

        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
